package com.wanshi.member.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wanshi.member.base.Result;
import com.wanshi.member.entity.Member;
import com.wanshi.member.entity.Staff;
import com.wanshi.member.req.MemberREQ;
import com.wanshi.member.req.StaffREQ;
import com.wanshi.member.service.IMemberService;
import com.wanshi.member.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工信息表 前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private IStaffService staffService;


    @PostMapping("/list/search/{page}/{size}")
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody StaffREQ req ) {
        return staffService.search(page, size, req);
    }


    /**
     * 新增会员
     * @param staff
     * @return
     */
    @PostMapping // post /member
    public Result add(@RequestBody Staff staff) {
        if(StringUtils.isBlank(staff.getPassword())){
            String password = new BCryptPasswordEncoder().encode("123456");
            staff.setPassword(password);
        }
        boolean b = staffService.saveOrUpdate(staff);
        if(b) {
            return Result.ok();
        }
        return Result.error("新增会员信息失败");
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        boolean b = staffService.removeById(id);
        if(b){
            return Result.ok();
        }
        return Result.error("删除员工信息失败");
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable int id) {
        Staff staff = staffService.getById(id);
        return Result.ok(staff);
    }



}
